package com.example.appiumautotest.utils;

import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.springframework.stereotype.Component;

import java.net.MalformedURLException;
import java.net.URL;

@Component
public class InitConnectInfo {

    DesiredCapabilities capabilities = new DesiredCapabilities();
    private AndroidDriver androidDriver;

    // 初始化capabilities信息
    public DesiredCapabilities setConnectInfo(String platform,
                                              String version,
                                              String name,
                                              String appPackage,
                                              String appActivity){
        capabilities.setCapability("platformName",platform);
        capabilities.setCapability("platformVersion",version);
        capabilities.setCapability("deviceName",name);
        // capabilities.setCapability("automationName","uiautomator1");
        capabilities.setCapability("resetKeyboard",true);
        capabilities.setCapability("appPackage",appPackage);
        capabilities.setCapability("appActivity",appActivity);
        capabilities.setCapability("noReset",true); // 不清除数据进行启动
        return capabilities;
    }

    // 获取android driver
    public AndroidDriver getAndroidDriver(String url,DesiredCapabilities cap){
        try {
            androidDriver = new AndroidDriver(new URL(url),cap);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return androidDriver;
    }
}
